package masterjava.common.http;

import masterjava.common.property.Property;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * User: GKislin
 * Date: 28.02.11
 */
public class HttpUtil {

    public static HttpParams createHttpParams(Property... props) {
        BasicHttpParams params = new BasicHttpParams();
        for (Property prop : props) {
            params.setParameter(prop.getKey(), prop.get());
        }
        return params;
    }

    public static Reader getReader(HttpEntity entity) throws IOException {
        if (entity.getContentEncoding() != null) {
            return new InputStreamReader(entity.getContent(), EntityUtils.getContentCharSet(entity));
        } else {
            return new InputStreamReader(entity.getContent());
        }
    }
}
